/**
  ******************************************************************************
  * File Name          : syscfg.c
  * Description        : Code for freertos applications
  ******************************************************************************
  * @attention
  *
  ******************************************************************************
  */

/* Includes ------------------------------------------------------------------*/
#include "syscfg.h"
#include "bsp.h"
#include "spiflash.h"
#include "collector.h"
#include <string.h>

SysCfg_ParameterType SysCfg_RunPara;
static SysCfg_StorageType   SysCfg_Storage;

SysRil_ParameterType SysRil_RunPara;
static SysRil_StorageType   SysRil_Storage;

static const SysCfg_StorageType  SysCfg_StorageInit = 
{
	.key = SYSCFG_STORAGE_KEY,
	
	.para.lora_num  = 1u,
	.para.rs485_num = 0u,
};

static const SysRil_StorageType  SysRil_StorageInit = 
{
	.key = SYSRIL_STORAGE_KEY,
	
	.para.year  = 21u,
	.para.month = 7u,
  .para.day   = 6u,
  .para.clnum = 0,
};

uint8_t SysCfg_CheckPara(SysCfg_ParameterType *para)
{
	if(para->lora_num > COLLECTOR_LORADEV_MAXNUM)
	{
		return 1;
	}
	
	if(para->rs485_num > COLLECTOR_LORADEV_MAXNUM)
	{
		return 2;
	}
	
	return 0;
}

uint8_t SysRil_CheckPara(SysRil_ParameterType *para)
{
	if((para->year > 99u) || (para->year < 20))
	{
		return 1;
	}
	
	if((para->month < 1u) || (para->month > 12))
	{
		return 2;
	}
	
  if((para->day < 1u) || (para->day > 31))
	{
		return 3;
	}
  
	return 0;
}

/***************************** LOAD *************************************/
static void syscfg_save_initpara(void)
{
	sFlash_SectorErase(SFLASH_SYSCFG_ADDR);
	
	sFlash_SectorAlignedProgram(SFLASH_SYSCFG_ADDR, (uint8_t *)&SysCfg_StorageInit, sizeof(SysCfg_StorageType));
	
	SysCfg_RunPara = SysCfg_StorageInit.para;
	
	SysUsrLog("SysCfg InitPara saved success!\r\n");
}

void SysCfg_SavePara(void)
{
	memset(&SysCfg_Storage, 0, sizeof(SysCfg_StorageType));
	
	sFlash_SectorErase(SFLASH_SYSCFG_ADDR);
	
	strncpy(SysCfg_Storage.key, SYSCFG_STORAGE_KEY, 8);
	
	SysCfg_Storage.para = SysCfg_RunPara;
	
	sFlash_SectorAlignedProgram(SFLASH_SYSCFG_ADDR, (uint8_t *)&SysCfg_Storage, sizeof(SysCfg_StorageType));
	
	SysUsrLog("SysCfg RunPara saved success!\r\n");
}

uint8_t SysCfg_LoadPara(void)
{
	memset(&SysCfg_Storage, 0, sizeof(SysCfg_StorageType));
	
	sFlash_AnywhereRead(SFLASH_SYSCFG_ADDR, (uint8_t *)&SysCfg_Storage, sizeof(SysCfg_StorageType));
	
	if(strcmp(SysCfg_Storage.key, SYSCFG_STORAGE_KEY) != 0)
	{
		syscfg_save_initpara();
		return 1;
	}
	
	if(SysCfg_CheckPara(&SysCfg_Storage.para) != 0)
	{
		syscfg_save_initpara();
		return 2;
	}
	
	SysCfg_RunPara = SysCfg_Storage.para;
	
	SysUsrLog("Load SysCfg ok!\r\n");
	
	return 0;
}

/***************************************************************/
int SysRil_Day_diff(int year_start, int month_start, int day_start, int year_end, int month_end, int day_end)
{
	int y2, m2, d2;
	int y1, m1, d1;
	
	m1 = (month_start + 9) % 12;
	y1 = year_start - m1/10;
	d1 = 365*y1 + y1/4 - y1/100 + y1/400 + (m1*306 + 5)/10 + (day_start - 1);

	m2 = (month_end + 9) % 12;
	y2 = year_end - m2/10;
	d2 = 365*y2 + y2/4 - y2/100 + y2/400 + (m2*306 + 5)/10 + (day_end - 1);
	
	return (d2 - d1);
}

static void sysril_save_initpara(void)
{
	sFlash_SectorErase(SFLASH_SYSRIL_ADDR);
	
	sFlash_SectorAlignedProgram(SFLASH_SYSRIL_ADDR, (uint8_t *)&SysRil_StorageInit, sizeof(SysRil_StorageType));
	
	SysRil_RunPara = SysRil_StorageInit.para;
	
	SysUsrLog("SysRil InitPara saved success!\r\n");
}

void SysRil_SavePara(void)
{
	memset(&SysRil_Storage, 0, sizeof(SysRil_StorageType));
	
	sFlash_SectorErase(SFLASH_SYSRIL_ADDR);
	
	strncpy(SysRil_Storage.key, SYSRIL_STORAGE_KEY, 8);
	
	SysRil_Storage.para = SysRil_RunPara;
	
	sFlash_SectorAlignedProgram(SFLASH_SYSRIL_ADDR, (uint8_t *)&SysRil_Storage, sizeof(SysRil_StorageType));
	
	SysUsrLog("SysRil RunPara saved success!\r\n");
}

uint8_t SysRil_LoadPara(void)
{
	memset(&SysRil_Storage, 0, sizeof(SysRil_StorageType));
	
	sFlash_AnywhereRead(SFLASH_SYSRIL_ADDR, (uint8_t *)&SysRil_Storage, sizeof(SysRil_StorageType));
	
	if(strcmp(SysRil_Storage.key, SYSRIL_STORAGE_KEY) != 0)
	{
		sysril_save_initpara();
		return 1;
	}
	
	if(SysRil_CheckPara(&SysRil_Storage.para) != 0)
	{
		sysril_save_initpara();
		return 2;
	}
	
	SysRil_RunPara = SysRil_Storage.para;
	
	SysUsrLog("Load SysRil ok!\r\n");
	
	return 0;
}
